package com.ruoyi.project.system.approval.controller;

import java.util.List;

import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.approval.domain.Approval;
import com.ruoyi.project.system.approval.service.IApprovalService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 审批资格Controller
 * 
 * @author stefan
 * @date 2021-11-10
 */
@Controller
@RequestMapping("/system/approval")
public class ApprovalController extends BaseController
{
    private String prefix = "system/approval";

    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("system:approval:view")
    @GetMapping()
    public String approval()
    {
        return prefix + "/approval";
    }

    /**
     * 查询审批资格列表
     */
    @RequiresPermissions("system:approval:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Approval approval)
    {
        startPage();
        List<Approval> list = approvalService.selectApprovalList(approval);
        return getDataTable(list);
    }

    /**
     * 导出审批资格列表
     */
    @RequiresPermissions("system:approval:export")
    @Log(title = "审批资格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Approval approval)
    {
        List<Approval> list = approvalService.selectApprovalList(approval);
        ExcelUtil<Approval> util = new ExcelUtil<Approval>(Approval.class);
        return util.exportExcel(list, "审批资格数据");
    }

    /**
     * 新增审批资格
     */
    @GetMapping("/add")
    public String add(Model model, Dept dept, User user) {
        List<Dept> depts = deptService.selectDeptList(dept);
        model.addAttribute("userlist",userService.selectUserList(user));
        model.addAttribute("deptList", deptService.selectDeptList(dept));
        return prefix + "/add";
    }

    /**
     * 新增保存审批资格
     */
    @RequiresPermissions("system:approval:add")
    @Log(title = "审批资格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Approval approval)
    {
        return toAjax(approvalService.insertApproval(approval));
    }

    /**
     * 修改审批资格
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Approval approval = approvalService.selectApprovalById(id);
        mmap.put("approval", approval);
        return prefix + "/edit";
    }

    /**
     * 修改保存审批资格
     */
    @RequiresPermissions("system:approval:edit")
    @Log(title = "审批资格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Approval approval)
    {
        return toAjax(approvalService.updateApproval(approval));
    }

    /**
     * 删除审批资格
     */
    @RequiresPermissions("system:approval:remove")
    @Log(title = "审批资格", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(approvalService.deleteApprovalByIds(ids));
    }
}
